package admin_handlers

import (
	"fmt"
	"github.com/iris-contrib/middleware/csrf"
	"github.com/kataras/iris"
	"github.com/siaoynli/goProject/services"
	"github.com/siaoynli/goProject/utils/serializer"
	"gopkg.in/go-playground/validator.v9"
)


func  GetAdminLogin(ctx iris.Context)   {
	ctx.ViewData(csrf.TemplateTag, csrf.TemplateField(ctx))
	ctx.View("admin/login/index.html")
}


func  PostAdminLogin(ctx iris.Context)   {
	ctx.JSON(serializer.Response{
		Status: 200,
		Msg:    "login",
	})
}

func  GetAdminRegister(ctx iris.Context)   {
	ctx.ViewData(csrf.TemplateTag, csrf.TemplateField(ctx))
	ctx.View("admin/login/register.html")
}


func  PostAdminRegister(ctx iris.Context)   {
	 service :=new(services.AdminRegisterService)
	 err := ctx.ReadForm(service)
	 if  err != nil {
	 	  fmt.Println("err:",err)
		  ctx.JSON(serializer.Response{
			 Status: 500,
			 Msg:    "服务器内部错误,请联系管理员",
		 })
		 return
	 }
	validate := validator.New()
	err = validate.Struct(service)

	if err !=nil {
		_,_ = ctx.JSON(serializer.Response{
			Status: 500,
			Msg:    "校验失败",
		})
		return
	}
	str := service.Register()

	if  str != "" {
		ctx.JSON(serializer.Response{
			Status: 200,
			Msg:    str,
		})
		return
	}

	ctx.JSON(serializer.Response{
		Status: 200,
		Msg:    "注册成功",
	})
}


func  GetAdminIndex(ctx iris.Context)   {
	ctx.View("admin/index/index.html")
}